package com.yonyou.gmmc.service.violationInquiry.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.common.annotation.ParamValid;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.violationInquiry.constant.ViolationInquiryConstant;
import com.yonyou.gmmc.service.violationInquiry.model.TmRegCities;
import com.yonyou.gmmc.service.violationInquiry.model.TmRegulationsThrid;
import com.yonyou.gmmc.service.violationInquiry.service.ViolationInquiryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = ViolationInquiryConstant.BASE_PATH + "/" + ViolationInquiryConstant.VERSION)
@Api("车行易-违章查询API")
public class ViolationInquiryController {
	private Logger logger = Logger.getLogger(ViolationInquiryController.class);

	@Autowired
	private ViolationInquiryService violationInquiryService;

	/**
	 * 违章查询
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/parse2City", method = RequestMethod.GET)
	@ApiOperation(value = "违章查询定时任务", notes = "违章查询定时任务")
	public Response parse2City() {
		logger.info("ViolationInquiryController.parse2City==>start...");
		try {
			violationInquiryService.parse2City();
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ViolationInquiryController.parse2City==>end...");
		}
	}
	/**
	 * 更新违章车辆信息
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/checkCarReg", method = RequestMethod.GET)
	@ApiOperation(value = "查询指定省 发动机号和车架号", notes = "查询指定省 发动机号和车架号")
	@ParamValid
	public Response<List<TmRegCities>> checkCarReg(@RequestParam(required=true,defaultValue="") String registNo) {
		logger.info("ViolationInquiryController.checkCarReg==>start...");
		try {
		    registNo = registNo.replaceAll(" ", "");
			Map<String,Object> map = new HashMap<>();
			map.put("cityCode", registNo);
			List<TmRegCities> list = violationInquiryService.checkCarReg(map);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ViolationInquiryController.checkCarReg==>end...");
		}
	}
	
	/**
	 * 更新违章车辆信息
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/updateCarReg", method = RequestMethod.GET)
	@ApiOperation(value = "查询违章信息", notes = "查询违章信息")
	@ParamValid
	public Response<Map<String, Object>> updateCarReg(@RequestParam(required=true,defaultValue="贵BAX383") String registNo
			,@RequestParam(required=false,defaultValue="") String vin
			,@RequestParam(required=false,defaultValue="") String engine
			,@RequestParam(value = "userId_",required = false,defaultValue = "") String userId_ 
			,HttpServletRequest request) {
		logger.info("ViolationInquiryController.updateCarReg==>start...");
		String userId = "";
		try {
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
			Map<String,String> map = new HashMap<>();
			map.put("registNo", registNo);
			map.put("vin", vin);
			map.put("engine", engine);
			map.put("userId", userId);
			Map<String, Object> list = violationInquiryService.updateCarReg(map);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ViolationInquiryController.updateCarReg==>end...");
		}
	}
	
	/**
	 * 进入违章页面前调用：校验车主
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/checkCar", method = RequestMethod.GET)
	@ApiOperation(value = "校验车主", notes = "校验车主")
	@ParamValid
	public Response<Map<String, Object>> checkCar(HttpServletRequest request
			,@RequestParam(value = "userId_",required = false,defaultValue = "") String userId_) {
		logger.info("ViolationInquiryController.checkCar==>start...");
		String userId = "";
		try {
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
			Map<String,Object> map = new HashMap<>();
			map.put("userId", userId);
			Map<String, Object> result = violationInquiryService.checkCar(map);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ViolationInquiryController.checkCar==>end...");
		}
	}
	
	/**
	 * 违章查询历史
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/findHistory", method = RequestMethod.GET)
	@ApiOperation(value = "违章查询历史", notes = "违章查询历史")
	@ParamValid
	public Response<List<TmRegulationsThrid>> findHistory(HttpServletRequest request
			,@RequestParam(value = "userId_",required = false,defaultValue = "") String userId_ ) {
		logger.info("ViolationInquiryController.findHistory==>start...");
		String userId = "";
		try {
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
			Map<String,Object> map = new HashMap<>();
			map.put("userId", userId);
			List<TmRegulationsThrid> result = violationInquiryService.findHistory(map);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ViolationInquiryController.findHistory==>end...");
		}
	}
	/**
	 * 违章查询历史
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/deleteHistory", method = RequestMethod.GET)
	@ApiOperation(value = "清空历史", notes = "清空历史")
	@ParamValid
	public Response<Integer> deleteHistory(HttpServletRequest request
			,@RequestParam(value = "userId_",required = false,defaultValue = "") String userId_) {
		logger.info("ViolationInquiryController.deleteHistory==>start...");
		String userId = "";
		try {
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
			Map<String,Object> map = new HashMap<>();
			map.put("userId", userId);
			Integer result = violationInquiryService.deleteHistory(map);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ViolationInquiryController.deleteHistory==>end...");
		}
	}
	
	/**
	 * 违章查询简称
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/findProvince", method = RequestMethod.GET)
	@ApiOperation(value = "违章查询简称", notes = "违章查询简称")
	@ParamValid
	public Response<List<Map<String,Object>>> findProvince() {
		logger.info("ViolationInquiryController.findProvince==>start...");
		try {
			List<Map<String,Object>> result = violationInquiryService.findProvince();
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("ViolationInquiryController.findProvince==>end...");
		}
	}
	
}
